// pages/my/order/order.js
var app = getApp();
var pagesize = 10;
var pageoffset;
var payinfo;
var orderindex;
var reasons;
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var ordertype = '1';
    if (options.ordertype) {
      ordertype = options.ordertype;
    }
    this.setData({
      ordertype: ordertype,
      orders: []
    });
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          scrollhgt: res.windowHeight,
          scrolltop: 0
        });
      },
    });
    pageoffset = 1;
    payinfo = [];
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.loadOrderlist(this.data.ordertype, pageoffset);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  loadMoreOrder: function () {
    // console.log(this.data.noMore);
    if (this.data.noMore == true) {
      return;
    }
    pageoffset++;
    this.loadOrderlist(this.data.ordertype, pageoffset);
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  changeOrderStatus: function (e) {
    var otype = e.currentTarget.dataset.otype;
    this.setData({
      ordertype: otype
    });
    pageoffset = 1;
    this.setData({
      noMore: false
    });
    this.loadOrderlist(otype, pageoffset);
  },
  loadOrderlist: function (ordertype, offset) {
    var url = '/yuanmai_we/orders/orderFormList/ALL';
    if (ordertype == '2') {
      url = '/yuanmai_we/orders/orderFormList/UNPAID';
    }
    else if (ordertype == '3') {
      url = '/yuanmai_we/orders/orderFormList/UNRECEIVED';
    }
    else if (ordertype == '4') {
      url = '/yuanmai_we/orders/orderFormList/REFUND';
    }
    url = url + '/' + pagesize + '/' + offset;
    url = url + '?userId=' + app.globalData.userid;
    app.appShowLoading(30000);
    app.loadDataFromServer(url, this.loadOrderlistCallBack, url, 'get', true)
  },
  loadOrderlistCallBack: function (res) {
    wx.hideToast();
    console.log(res);
    if (res.length == null || res.length == undefined) {
      return;
    }
    console.log(res.length, pagesize);
    if (res.length < pagesize || res.length <= 0) {
      this.setData({
        noMore: true
      });
    }
    var orderlist = [];
    if (pageoffset > 1) {
      orderlist = this.data.orders;
    }
    else {
      this.setData({
        scrolltop: 0
      });
      payinfo = [];
    }
    for (var i = 0; i < res.length; i++) {
      var goods = [];
      for (var j = 0; j < res[i].orderformOrderBillDOs[0].orderformOrderDetailDOs.length; j++) {
        var goodsinfo = {
          img: res[i].orderformOrderBillDOs[0].orderformOrderDetailDOs[j].IMAGE,
          num: res[i].orderformOrderBillDOs[0].orderformOrderDetailDOs[j].COUNT,
          name: res[i].orderformOrderBillDOs[0].orderformOrderDetailDOs[j].NAME,
          price: res[i].orderformOrderBillDOs[0].orderformOrderDetailDOs[j].PRICE
        }
        goods.push(goodsinfo);
      }
      var orderinfo = {
        orderno: res[i].id,
        corderno: res[i].orderformOrderBillDOs[0].id,
        source: res[i].orderformBatchDO.ORDERSOURCE,
        status: res[i].orderformOrderBillDOs[0].orderformOrderDO.STATUS,
        goods: goods
      }
      orderlist.push(orderinfo);
      payinfo.push(res[i]);
    }
    this.setData({
      orders: orderlist
    });
  },
  goBuyAction: function () {
    wx.switchTab({
      url: '../../index/index',
    });
  },
  payOrder: function (e) {
    console.log('pay');
    var index = e.currentTarget.dataset.itemindex;
    var orderinfo = encodeURIComponent(JSON.stringify(payinfo[index]));
    wx.navigateTo({
      url: '../../pay/pay?order=' + orderinfo,
    });
  },
  showDetail:function(e){
    var index = e.currentTarget.dataset.itemindex;
    orderindex = index;
    var orderno = this.data.orders[index].orderno;
    var url = '/yuanmai_we/orders/mcake/status/query?id='+orderno;
    app.loadDataFromServer(url,this.orderDetailCallBack,{},'get',true);
  },
  orderDetailCallBack:function(res){
    console.log(res);
    var orderinfo = encodeURIComponent(JSON.stringify(payinfo[orderindex]));
    wx.navigateTo({
      url: './orderdetail?order=' + orderinfo + '&status=' + res.orderStatus,
    });
  },
  cancelOrder:function(e){
    var index = e.currentTarget.dataset.itemindex;
    orderindex = index;
    var that = this;
    wx.showModal({
      title: '',
      content: '是否取消订单？',
      confirmText: '是',
      confirmColor: '#a08257',
      cancelText:'否',
      success:function(res){
        // console.log(res);
        if(res.confirm){
          that.didCancelAction1();
        }
      }
    });
  },
  didCancelAction1:function(){
    var url = '/yuanmai_we/orderform/order/cancel/reason';
    app.loadDataFromServer(url,this.cancelCallBack1,{},'get')
  },
  cancelCallBack1:function(res){
    console.log(res);
    reasons = res;
    var reasons_ch = [];
    for(var i=0;i<reasons.length;i++){
      reasons_ch.push(reasons[i].REASON);
    }
    var that = this;
    wx.showActionSheet({
      itemList: reasons_ch,
      success: function(res){
        that.didCancelAction2(res.tapIndex);
      }
    });
  },
  didCancelAction2:function(index){
    var url = '/yuanmai_we/orderform/orderForm/cancel/';
    var orderno = this.data.orders[orderindex].corderno;
    url = url + orderno;
    var params = {
      REASON:reasons[index].REASON,
      __zaofans: true
    }
    app.loadDataFromServer(url,this.cancelCallBack2,params,'post',true);
  },
  cancelCallBack2:function(res){
    console.log(res);
    if(res.success == true){
      this.loadOrderlist(this.data.ordertype, pageoffset);
    }
  }
})